package usw.webserver.entity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 *
 *
 */
public class HttpRequest {

	private static Logger log = Logger.getLogger(HttpRequest.class);

	List<String> headers = new ArrayList<String>();

	public Method method = new Method();

	public String uri;

	public String version;

	/**
	 * 
	 * @param is
	 * @throws IOException
	 */
	public HttpRequest(InputStream is) throws IOException 
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		
		//	parse request line
		String str = reader.readLine();
		parseRequestLine(str);
		
		//	parse request header
		while (!str.equals("")) 
		{
			str = reader.readLine();
			
			parseRequestHeader(str);
		}
	}

	/**
	 * 
	 * @param str
	 */
	private void parseRequestLine(String str) {
		
		log.info(str);
		
		String[] split = str.split(" ");
		
		method.setStrMethod(split[0]);
		
		uri = split[1];
		version = split[2];
	}

	/**
	 * 
	 * @param str
	 */
	private void parseRequestHeader(String str) 
	{
		log.info(str);
		
		headers.add(str);
	}
}
